#include "TdMedia.h"

uint32_t TdMedia::init(MediaParam_t param)
{
  
    mRtp.Link(mCodec);
    mCodec.Link(mRtp);
    chan = param.channel;
    mCodec.init(VideoH264);
    mCodec.setParam(param.codecParam);
    //mCodec.init(VideoH264);
    
    if(mRtp.setUp(param.channel,param.destIp,param.destPort,param.basePort))
    {
        spdlog::error("RTP 启动失败");
        return -1 ;
    }
    return 0 ;
}
TdRtp* TdMedia::getRtpHandle()
{
    return &mRtp;
}
TdCodec* TdMedia::getCodecHandle()
{
    return &mCodec ;
}

uint32_t TdMedia::start()
{
    if(chan.empty())
    {
        exit(1); //todo
    }
    fifoMsg = new FifoMsgSession();
    if(fifoMsg->Create(chan))
    {
        exit(0);
    }
    spdlog::debug("Rtp 启动成功\r\n");
    CodecParam *param = nullptr ;
    FifoMsg * ffMsgPtr=nullptr;
    while(1)
    {
        if(!fifoMsg->Wait())
        {
            ffMsgPtr = fifoMsg->GetFifoMsg();
            //printf("ffMsgPtr->cmd: %d \n",ffMsgPtr->cmd);
            if(ffMsgPtr->cmd = ModifyCodecParam)
            {
                param = (CodecParam*)ffMsgPtr->content;
                //printf("get: %d %d %d %d \n",param->height,param->width,param->maxBitrate,param->rate);
                //printf("%s: FIFO read %s \n",fifoStr.c_str(),buf_r);
                getCodecHandle()->setParam(*param); //设置编码参数 ;
                //setParam(*param); //设置编码参数 ;
            }
        }
    }
    return 0 ;
}